﻿Shader "Unlit/EdgeDetection"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _EdgePower("EdgePower（边缘强度）",Float) = 1.0
        _EdgeColor("EdgeColor（边缘颜色）",Color) = (0,0,0,1)
        _BackgroundColor("Background（背景颜色）",Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            ZTest Always Cull Off ZWrite Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct a2v
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                half2 uv[9] : TEXCOORD0;
            };

            sampler2D _MainTex;
            half4 _MainTex_TexelSize; //Unity提供的访问 _MainTex 纹理每个纹素的大小
            fixed _EdgePower;
            fixed4 _EdgeColor;
            fixed4 _BackgroundColor;

            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                half2 uv = v.texcoord;
                o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1,-1);
                o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0,-1);
                o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1,-1);
                o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1,0);
                o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0,0);
                o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1,0);
                o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1,1);
                o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0,1);
                o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1,1);
                return o;
            }

            //计算亮度（饱和度为0的颜色值）
            fixed Luminance(fixed4 color)
            {
                return 0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b;
            }
            //使用 Sobel 算子计算 Gradient
            half Sobel(v2f i)
            {
                const half Gx[9] = {-1,-2,-1,
                                    0,0,0,
                                    1,2,1};
                const half Gy[9] = {-1,0,1,
                                    -2,0,2,
                                    -1,0,1};
                half texColor;
                half edgeX = 0;
                half edgeY = 0;
                for(int k = 0; k < 9 ; k++)
                {
                    texColor = Luminance(tex2D(_MainTex,i.uv[k])); //采样 当前uv 周围像素的颜色
                    edgeX += texColor * Gx[k]; //和Sobel算子各分量权重相乘、求和
                    edgeY += texColor * Gy[k];
                }
                half edge = 1 - abs(edgeX) - abs(edgeY); //edge越小越可能是边缘
                return edge;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                half edge = Sobel(i); //edge越小越可能是边缘
                fixed4 originEdgeColor = lerp(_EdgeColor,tex2D(_MainTex,i.uv[4]),edge); //边缘接近原图
                fixed4 solidEdgeColor = lerp(_EdgeColor,_BackgroundColor,edge); //边缘接近纯色
                return lerp(originEdgeColor,solidEdgeColor,_EdgePower);
            }
            ENDCG
        }
    }
    Fallback Off
}
